package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Category;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.SetmealDishMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 保存套餐信息
     */
    @PostMapping()
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //提交的JSON数据中不仅有setmeal数据内容 还有List<setmealDishes>
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithSetmeal(setmealDto);
        return R.success("新增套餐成功!");
    }

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R<Page> selectAll(int page,int pageSize,String name){
        //先查询套餐
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //模糊查询姓名
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,lqw);//这里除了套餐分类名称没有返回 其他都返回了
        //现在要做的就是返回套餐分类名的dto

        Page<SetmealDto> pageDto = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageDto,"records");//copy排除掉结果
        //结果需要自己来配置好 配置套餐分类 套餐的分类可以通过category_id来获取到
        List<Setmeal> records = pageInfo.getRecords();//根据套餐记录查到套餐信息
        List<SetmealDto> setmealDtos = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //把setmeal对象拷贝到Dto中
            BeanUtils.copyProperties(setmeal, setmealDto);
            //然后把套餐分类名称放进去;
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        //把结果放进去
        pageDto.setRecords(setmealDtos);
        return R.success(pageDto);
    }

    //整个专业的逻辑删除试试
    /**
     * 套餐删除
     */
    @DeleteMapping()
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info("删除的id有: "+ids.toString());
        //删除setmeal_dish表的
        setmealService.removeWithSetmeal(ids);
        return R.success("删除成功");
    }

    /**
     * 套餐启用停售
     */
    @PostMapping("/status/{status}")
    public R<String> halt(@RequestParam List<Long> ids,@PathVariable int status){
        log.info("套餐操作 0停售 1启用 操作: "+status+" ids = "+ids);
        //查到套餐
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        List<Setmeal> setmeals = setmealService.list(lqw);
        //改动状态
        List<Setmeal> setmealsOver = setmeals.stream().map(setmeal -> {
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(setmealsOver);
        return R.success("修改状态成功");
    }

    /**
     * 套餐修改回显
     */
    @GetMapping("/{id}")
    public R<SetmealDto> setmealModify(@PathVariable Long id){
        log.info("修改套餐id :" +id);
        //根据id找到套餐Dto
        SetmealDto setmealDto = setmealService.selelctWithSetmealDto(id);
        return R.success(setmealDto);
    }

    /**
     * 套餐修改保存
     */
    @PutMapping()
    public R<String> modifySave(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        //要讲dto分开把东西存储下来
        setmealService.modifyandSave(setmealDto);
        return R.success("保存成功!");
    }


    /**
     * 移动端套餐展示
     */
    @GetMapping("/list")
    public R<List<Setmeal>> dispalySetmeal(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(lqw);
        return R.success(setmeals);


    }

    /**
     * 移动端套餐规格
     */
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> setmealGG(@PathVariable Long  id){
        //这里的id是setmeal的id 不是分类id也不是菜品id
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //先查套餐 然后查套餐菜品表
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal!=null,SetmealDish::getSetmealId,setmeal.getId());
        lqw.orderByAsc(SetmealDish::getSort).orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(lqw);

        return R.success(list);
    }


}
